package com.jucai.pppeiyouclient.view.fragment;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.NegParAgreedAdapter;
import com.jucai.pppeiyouclient.base.BaseFragment;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.ParticipantOrderBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Kuky on 2017-05-05.
 */

public class NegParAgreedFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "NegParAgreedFragment : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView partsRv;
    private List<ParticipantOrderBean> participants = new ArrayList<>();
    private NegParAgreedAdapter partAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView() {

        partAdapter = new NegParAgreedAdapter(getActivity(), participants);
        refreshLayout = (PullToRefreshLayout) itemView.findViewById(R.id.refresh_view);
        partsRv = (PullToRefreshForRecyclerView) itemView.findViewById(R.id.content_view);

        partsRv.setLayoutManager(new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false));
        partsRv.setItemAnimator(new DefaultItemAnimator());
        partsRv.setAdapter(partAdapter);

        partAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {

            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Activity.getapplylist");
        param.put("id", getActivity().getIntent().getStringExtra("activity_id"));
        param.put("page", "1");
        LogUtils.e(TAG + "param = " + param);

        CommonRequestNetData.getJsonObjectData(getActivity(), param, true,new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                try {
                    JSONArray agreeArray = jsonObject.getJSONArray("agree");
                    participants = new Gson().fromJson(agreeArray.toString(), new TypeToken<List<ParticipantOrderBean>>() {
                    }.getType());
                    LogUtils.e(TAG + "parts = " + participants);
                    partAdapter.setData(participants);
                } catch (Exception e) {
                    ToastUtils.showToast("暂无用户！");
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected int getFragmentViewId() {
        return R.layout.common_fragment_with_ptr;
    }

    @Override
    public void onClick(View view) {

    }


    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Conductor_Activity.getapplylist");
                param.put("id", getActivity().getIntent().getStringExtra("activity_id"));
                param.put("page", page + "");
                LogUtils.e(TAG + "param = " + param);

                CommonRequestNetData.getJsonObjectData(getActivity(), param, true,new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        LogUtils.e(TAG + "jsObj = " + jsonObject.toString());
                        try {
                            JSONArray agreeArray = jsonObject.getJSONArray("agree");
                            List<ParticipantOrderBean> list = new Gson().fromJson(agreeArray.toString(), new TypeToken<List<ParticipantOrderBean>>() {
                            }.getType());
//                            participants.addAll(list);
                            LogUtils.e(TAG + "parts = " + participants);
                            partAdapter.setData(participants);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
